package com.gaga.console.core.controller.sys;


import com.gaga.common.path.ApiPath;
import com.gaga.common.result.Result;
import com.gaga.console.base.controller.BaseController;
import com.gaga.console.base.entity.GenCode;
import com.gaga.console.base.utils.MybatisPlusGenerator;
import com.gaga.console.core.entity.SysDatabase;
import com.gaga.console.core.service.SysDatabaseService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


/**
 * 前端控制器
 *
 * @author liujia
 * @since 2022-06-01
 */
@Api(tags = "系统模块-代码生成")
@RestController
@RequestMapping(ApiPath.ROOT + "/sys/gen")
public class GenCodeController extends BaseController {
    @Autowired
    private SysDatabaseService sysDatabaseService;

    /**
     * 生成代码
     */
    @ApiOperation(value = "生成代码")
    @PostMapping("/code/{dbId}/{tableName}")
    public Result genCode(@PathVariable String dbId, @PathVariable String tableName) {
        
        SysDatabase sysDatabase = sysDatabaseService.getById(dbId);
        if (sysDatabase == null) {
            return Result.fail("数据库信息有误");
        }

        GenCode genCode = new GenCode();
        genCode.setUrl(sysDatabase.getUrl()).setUserName(sysDatabase.getUsername())
                .setPassword(sysDatabase.getPassword()).setTableName(tableName);
        MybatisPlusGenerator.genCode(genCode);

        return Result.ok();
    }

}
